<template>
  <div class="app-container">
    <div class="search-content">
      <div class="title">管理员查询</div>
      <div class="search-area">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input v-model="formInline.email" placeholder="用户名" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitByUser">查询</el-button>
          </el-form-item>
          <el-form-item label="ID" class="by-user">
            <el-input v-model="formInline.id" placeholder="id" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitById">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template v-slot="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" width="300">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center">
        <template slot-scope="scope">
          {{ scope.row.gender }}
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" width="180">
        <template slot-scope="scope">
          {{ scope.row.tel }}
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center">
        <template slot-scope="scope">
          {{ scope.row.integral_count }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.user_status | statusFilter">{{ scope.row.user_status === 0 ? '正常' : '冻结' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="avatar" label="头像" width="300">
        <template slot-scope="scope">
          <span>{{ scope.row.avatar }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ format(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  name: 'Member',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      formInline: {},
      listLoading: true,
      list: null,
      currentPage: 1,
      pageSize: 10,
      total: null
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async onSubmitByUser() {
      const res = await this.$http.get(`/cms/user/getByEmail?email=${this.formInline.email}`)
      this.list = res.data.rows
      this.total = res.data.count
    },
    async onSubmitById() {
      const res = await this.$http.get(`/cms/user/getById?id=${this.formInline.id}`)
      this.list = res.data.rows
      this.total = res.data.count
    },
    handleEdit(row) {},
    async handleDelete(row) {
      const res = await this.$http.delete(`/cms/user/delete/${row.id}`)
      if (res.code === 0) {
        await this.getUserList()
        this.$message({
          message: res.message,
          type: 'success'
        })
      }
    },
    async getUserList() {
      const res = await this.$http.get(`/cms/user/allMember?offset=${this.currentPage}&limit=${this.pageSize}`)
      this.list = res.data.rows
      this.total = res.data.count
      this.listLoading = false
    },
    format(date) {
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>
<style lang="scss" scoped>
  .app-container{
    .search-area{
      margin-top: 20px;
    }
  }
</style>
